/**
 * 
 */
package ender.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * 
 * @author chenxiwen
 * @date 2017年7月26日下午4:57:54
 */
public final class TimeUtils {
	
	public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	
	public static final String DATE_FORMAT = "yyyy-MM-dd";
	
	private static SimpleDateFormat dateTimeFormatter = new SimpleDateFormat(DATE_TIME_FORMAT, Locale.CHINA);
	
	private static SimpleDateFormat dateFormatter = new SimpleDateFormat(DATE_FORMAT, Locale.CHINA);
	
	private static SimpleDateFormat formatter = new SimpleDateFormat();
	
	private static Calendar calendar = Calendar.getInstance(Locale.CHINA);
	
	public static String format(String pattern, Date date) throws NullPointerException, IllegalArgumentException{
		formatter.applyPattern(pattern);
		formatter.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		return formatter.format(date);
	}
	
	public static String format(String pattern, long millis) throws NullPointerException, IllegalArgumentException{
		formatter.applyPattern(pattern);
		formatter.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		return formatter.format(new Date(millis));
	}
	
	public static Date parse(String pattern, String dateStr) throws NullPointerException, IllegalArgumentException, ParseException{
		formatter.applyPattern(pattern);
		formatter.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		return formatter.parse(dateStr);
	}
	
	public static String nowDateTime(){
		return dateTimeFormatter.format(new Date());
	}
	
	public static String nowDate(){
		return dateFormatter.format(new Date());
	}
	
	public static String getDateTime(Date date){
		return dateTimeFormatter.format(date);
	}
	
	public static String getDateTime(long millis){
		return dateTimeFormatter.format(new Date(millis));
	}
	
	public static Date fromDateTimeStr(String datetimeStr) throws ParseException{
		return dateTimeFormatter.parse(datetimeStr);
	}
	
	public static Date fromDateStr(String dateStr) throws ParseException{
		return dateFormatter.parse(dateStr);
	}
	
	public static Date fromMillis(long mills){
		return new Date(mills);
	}
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(format(TimeUtils.DATE_TIME_FORMAT, new Date()));
	}

}
